package cn.kgc.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * 事务工具类
 */
@Component("transactionUtil")
public class TransactionUtil {
    @Autowired
    private ConnectionUtil connectionUtil;

    /**
     * 开始事务
     * @throws SQLException
     */
    public void beginTransaction() throws SQLException {
        System.out.println("事务开始了");
        connectionUtil.getConnectin().setAutoCommit(false);
    }
    /**
     * 提交事务
     * @throws SQLException
     */
    public void commitTransaction() throws SQLException {
        System.out.println("提交事务");
        connectionUtil.getConnectin().commit();
    }
    /**
     * 回滚事务
     * @throws SQLException
     */
    public void rollbackTransaction() throws SQLException {
        System.out.println("回滚事务");
        connectionUtil.getConnectin().rollback();
    }
    /**
     * 释放连接
     * @throws SQLException
     */
    public void closeTransaction() throws SQLException {
        System.out.println("释放资源");
        connectionUtil.getConnectin().close();
        connectionUtil.removeConnection();
    }
}
